﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace Survey.Entity.Services
{
    public class MenuItem : IControllerAccess, ITVDataItem
    {
        #region members
        IList<MenuItem> _grantedMenuItems = new List<MenuItem>();
        IList<RoleAccess> _roleRights = new List<RoleAccess>();
        #endregion members

        public virtual bool IsExpanded { get; set; }

        #region collections
        /// <summary>
        ///     The list of all ControllerOrNode instances coming from the Menu.Config
        /// </summary>
        public virtual IList<MenuItem> GrantedMenuItems
        {
            get { return _grantedMenuItems; }
            set
            {
                Contract.Requires(value.Is(), " Children cannot be set to null");
                _grantedMenuItems = value;
            }
        }

        /// <summary>
        ///     The role set of the Roles which are granted to access this controller.
        ///     More grained setting for actions is contained in the RoleRights instance
        /// </summary>
        public virtual IList<RoleAccess> Roles
        {
            get { return _roleRights; }
            set
            {
                Contract.Requires(value.Is(), " RoleRights cannot be set to null");
                _roleRights = value;
            }
        }
        #endregion collections

        #region IController
        string IControllerAccess.ControllerName { get { return ControllerName; } }
        protected virtual string ControllerName { get { return ID; } }

        IEnumerable<IRoleAccess> IControllerAccess.Roles
        {
            get { return Roles.Select(role => role as IRoleAccess); }
        }
        #endregion IController

        #region ITVDataItem Members
        public virtual string ID { get; set; }
        public virtual string ParentId { get; set; }

        public virtual string Text { get; set; }
        public virtual string Href { get; set; }
        public virtual string ImagePath { get; set; }

        IEnumerable<ITVDataItem> ITVDataItem.Items
        {
            get { return Items; }
        }
        protected virtual IEnumerable<ITVDataItem> Items
        {
            get
            {
                return GrantedMenuItems
                    .Where(mi => ID.IsEqual(mi.ParentId))
                    .Select(cn => cn as ITVDataItem)
                    .ToList();
            }
        }

        /// <summary>
        ///     Url path extenders for other then default 'controller/action/id'
        /// </summary>
        IDictionary<string, object> ITVDataItem.RouteValues
        {
            get { return RouteValues; }
        }
        public virtual IDictionary<string, object> RouteValues
        {
            get { return null; }
        }
        #endregion

        #region Clone
        public virtual MenuItem Clone(IEnumerable<string> grantedRoles)
        {
            var mi = MemberwiseClone() as MenuItem;
            mi.Roles =
                Roles.Where(r => grantedRoles.Any(role => role.Equals(r.RoleName, StringComparison.Ordinal))).ToList();
            return mi;
        }

        public virtual MenuItem Clone(string currentRole)
        {
            var mi = MemberwiseClone() as MenuItem;
            mi.Roles = Roles.Where(r => r.RoleName.Equals(currentRole, StringComparison.Ordinal)).ToList();
            return mi;
        }
        #endregion Clone
    }
}